<?php
namespace app\sendmsg\model;
use app\BaseModel;
use app\card\model\CardValue;
use app\card\model\Collection;
use app\card\model\UserMark;

class UserInfo extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_user_info';
    public function cardList ($where)
    {
        $data = $this->where($where)->find();
        return $data;
    }
	
	public function getUserPhone($user_id ,$uniacid = null)
	{
		$filter = ['fans_id' => $user_id];
		if(!empty($uniacid)) $filter['uniacid'] = $uniacid;
		$result = $this->where($filter)->field('phone,wechat')->find();
		if(!empty($result)) $result = $result->toArray();
		return $result;
	}
    //获取员工
    public function userStaffAll($where){
        $data = $this->alias( 'a' )
            ->leftjoin( 'longbing_card_formId b', 'a.fans_id = b.user_id')
            ->join( 'longbing_card_user c', 'a.fans_id = c.id')
            ->field('c.id,c.openid,c.is_staff,c.uniacid,b.formId,b.id as bid,a.ww_account,a.fans_id')
            ->where($where)
            ->group('a.id')
            ->select()->toArray();
        return $data;
    }
    public function getCompanyUser($company_id){
        return $this->where('company_id',$company_id)->where('status',1)->where('is_staff',1)->group('company_id')->column('company_id','id');
    }
    public function userStaff($where){
        $data = $this->alias( 'a' )
            ->join( 'longbing_card_user b','a.fans_id = b.id')
            ->where($where)
            ->count();
        return $data;
    }
   //小程序小面公司的总人数
    public function getAllCompanyUser($where){
        $data = $this->alias( 'a' )
            ->join( 'longbing_card_company b', 'a.company_id = b.id')
            ->where($where)
            ->count();
        return $data;
    }
   public function getUserlist($where){
        return $this->where($where)->where('is_staff',1)->column('fans_id');
   }
    public function getzhlist($where,$page=1,$indexinpage=10,$type =0,$desc = 0){
        if($desc == 0){
            $de1 = SORT_ASC;
        }else{
            $de1 = SORT_DESC;
        }
        $data = $this->alias('a')
            ->join( 'longbing_card_user b', 'a.fans_id = b.id')
            ->join( 'longbing_card_job c', 'c.id = a.job_id ','left')
            ->field('a.company_id,a.fans_id as to_uid,a.uniacid,a.name,a.avatar,c.name as job_name,nickName,avatarUrl')
            ->where($where)
            ->select()->toArray();
        if($data){
            $collect = new Collection();
            $admin_goods = new CardShopOrder();
            $user_mark = new UserMark();
            foreach ($data as $k=>$v){
                if(!$v['name']){
                    $data[$k]['name'] = $v['nickName'];
                }
                if(!$v['avatar']){
                    $data[$k]['avatar'] = $v['avatarUrl'];
                }
                if(!$v['job_name']){
                    $data[$k]['job_name'] = '未设置职位';
                }
                if($type ==0){
                    $where = [
                        ['user_id','=',$v['to_uid']],
                        ['mark','=',1],
                        ['status','=',1],
                        ['uniacid','=',$v['uniacid']]
                    ];
                    $where1= [
                        ['company_id','=',$v['company_id']],
                        ['to_uid','=',$v['to_uid']],
                        ['uniacid','=',$v['uniacid']]
                    ];
                    $sales_count = $admin_goods->getGoosSale($where1);

                    //累计线索
                    $data[$k]['list'] = [
                        [
                            'name'=>'累计线索',
                            $dd = $collect->xian(['intention'=>0,'to_uid'=>$v['to_uid'],'uniacid'=>$v['uniacid']]),
                            $arr[] = $dd,
                            'value'=> $dd
                        ],[
                            'name'=>'累计客户',
                            $dd = $collect->xian(['intention'=>1,'to_uid'=>$v['to_uid'],'uniacid'=>$v['uniacid']]),
                            $arr[] = $dd,
                            'value'=> $dd
                        ],
                        [
                            'name'=>'昨日新增客户',
                            $dd = $collect->Yesterday(['a.intention'=>1,'a.to_uid'=>$v['to_uid'],'a.uniacid'=>$v['uniacid']]),
                            $arr[] = $dd,
                            'value'=> $dd
                        ],
                        [
                            'name'=>'累计跟进',
                            $dd = $user_mark->getCount($where),
                            $arr[] = $dd,
                            'value'=> $dd
                        ],
                        [
                            'name'=>'成交订单',
                            $dd = $sales_count['number'],
                            $arr[] = $dd,
                            'value'=> $dd
                        ],[
                            'name'=>'成交金额',
                            $dd = $sales_count['money'],
                            $arr[] = $dd,
                            'value'=> $dd
                        ]
                    ];
                    $card_value = new CardValue();
                    $rest = $card_value->bossGetAiValue($v['to_uid'],$v['uniacid']);
                    $data[$k]['total_info'] = $rest['data'];
                    $data[$k]['total'] =  $rest['max'];
                }
                if($type ==5){
                    $where2= [
                        ['company_id','=',$v['company_id']],
                        ['to_uid','=',$v['to_uid']],
                        ['uniacid','=',$v['uniacid']]
                    ];
                    $sales_count = $admin_goods->getGoosSale($where2);
                    $data[$k]['number'] = $sales_count['number'];
                    $data[$k]['money'] = $sales_count['money'];
                }
            }
            if($type == 0){
                if($data){
                    $last_names = array_column($data,'total');
                    array_multisort($last_names,$de1,$data);
                    $totle = $data[0]['total'];
                    if($totle<=0){
                        $totle =1;
                    }
                    foreach ($data as $k=>$v){
                        $data[$k]['rate'] = round($v['total']/$totle* 100,2);
                    }

                }
            }
            if($type == 5){
                if($data){
                    $last_names = array_column($data,'number');
                    array_multisort($last_names,$de1,$data);
                }
            }
        }

        $count = count($data);//总条数
        $start=($page-1)*$indexinpage;//偏移量，当前页-1乘以每页显示条数
        $data = array_slice($data,$start,$indexinpage);
        $totalPage = ceil(($count + $indexinpage-1) / $indexinpage);
        $data = formatNumberPrice($data,['total']);
        $data = transImagesOne($data,['avatar']);
        $arrs = [
            'total'=>$count,
            'per_page'=>$indexinpage,
            'current_page'=>$page,
            'data'=>$data,
            'last_page'=>$totalPage
        ];
        return $arrs;
    }
    //查询公司列表下的员工数量
    public function getCoampanyStaff($company_id){
        return $this->whereIn('company_id',$company_id)->where('is_staff',1)->where('status',1)->count();
    }
}